package com.showboom.elauncher.services;

import android.accessibilityservice.AccessibilityService;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityWindowInfo;

import com.showboom.elauncher.activity.MainActivity;
import com.showboom.elauncher.receiver.WaKuangStatusReceiver;
import com.showboom.elauncher.util.Util;

import java.util.List;

public class ShowboomService extends AccessibilityService {

    private static final String TAG = "leihuiming";
    private static final String WECHAT_LAUNCHER_UI = "com.tencent.mm.ui.LauncherUI";
    private static final String WECHAT_LOGIN_VOICE_UI = "com.tencent.mm.plugin.account.ui.LoginVoiceUI";
    private static final String WECHAT_LOGIN_SMS_UI = "com.tencent.mm.plugin.account.ui.LoginSmsUI";
    private static final String WECHAT_LOGIN_PASSWORD_UI = "com.tencent.mm.plugin.account.ui.LoginPasswordUI";
    private static final String WECHAT_CONTACT_INFO_UI = "com.tencent.mm.plugin.profile.ui.ContactInfoUI";
    private static final String WECHAT_NEW_FRIEND_UI = "com.tencent.mm.plugin.subapp.ui.friend.FMessageConversationUI";
    private static final String WECHAT_PENGYOUQUAN = "com.tencent.mm.plugin.sns.ui.SnsTimeLineUI";
    private static final String WECHAT_SETTINGS = "com.tencent.mm.plugin.setting.ui.setting.SettingsUI";
    private static final String WECHAT_MALL = "com.tencent.mm.plugin.mall.ui.MallIndexUI";
    private static final String WECHAT_DIAOXIAN = "com.tencent.mm.ui.widget.a.c";
    private static final String WECHAT_MOBILE_INPUT_UI = "com.tencent.mm.plugin.account.ui.MobileInputUI";
    private static final String WECHAT_LOGIN_UI = "com.tencent.mm.plugin.account.ui.LoginUI";
    private static final String WECHAT_REG_UI = "com.tencent.mm.plugin.account.ui.RegByMobileRegAIOUI";

    private static final String WECHAT_SIGNIN = "signin";
    private static final String WECHAT_UNSIGNIN = "unsignin";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "ShowboomService is create.");
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        int type = event.getEventType();
        switch (type) {
            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
                //Log.d(TAG, "TYPE_WINDOW_CONTENT_CHANGED = " + event.getClassName());
                break;
            case AccessibilityEvent.TYPE_WINDOWS_CHANGED:
                //Log.d(TAG, "TYPE_WINDOWS_CHANGED = " + event.getClassName());
                break;
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                //Log.d(TAG, "TYPE_WINDOW_STATE_CHANGED = " + event.getClassName());
                switch (event.getClassName().toString()) {
                    case WECHAT_LAUNCHER_UI:
                        if (WECHAT_SIGNIN.equals(checkMMLauncherUI())) {
                            sendWeChatSignState(true);
                        } else if (WECHAT_UNSIGNIN.equals(checkMMLauncherUI())) {
                            sendWeChatSignState(false);
                        }
                        break;
                    case WECHAT_CONTACT_INFO_UI:
                    case WECHAT_NEW_FRIEND_UI:
                    case WECHAT_PENGYOUQUAN:
                    case WECHAT_SETTINGS:
                    case WECHAT_MALL:
                        sendWeChatSignState(true);
                        break;
                    case WECHAT_LOGIN_VOICE_UI:
                    case WECHAT_LOGIN_SMS_UI:
                    case WECHAT_LOGIN_PASSWORD_UI:
                    case WECHAT_MOBILE_INPUT_UI:
                    case WECHAT_LOGIN_UI:
                    case WECHAT_REG_UI:
                        sendWeChatSignState(false);
                        break;
                }
                break;
        }
    }

    private void sendWeChatSignState(boolean signin) {
        Intent signinIntent = new Intent(WaKuangStatusReceiver.ACTION_SIGNIN_SHOWBOOM);
        signinIntent.putExtra("signin", signin);
        sendBroadcast(signinIntent);
    }

    @Override
    public void onInterrupt() {
        Log.d(TAG, "Showboom Accessibility service onInterrupt");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Showboom Accessibility service onDestroy");
    }

    public String isSignin(String signinId, String regId) {
        AccessibilityNodeInfo rowNode = getRootInActiveWindow();
        if (rowNode != null) {
            List<AccessibilityNodeInfo> btnSignin = rowNode.findAccessibilityNodeInfosByViewId(signinId);
            List<AccessibilityNodeInfo> btnReg = rowNode.findAccessibilityNodeInfosByViewId(regId);
            //Log.d("leihuiming", "btnSignin = " + btnSignin.size() + "  btnReg= " + btnReg.size());
            if (btnSignin.size() > 0 && btnReg.size() > 0) {
                return "unsignin";
            } else {
                return "signin";
            }
        } else {
            return "";
        }
    }

    public String checkMMLauncherUI() {
        String mmVersion = Util.getPackageVersion("com.tencent.mm");
        Log.d(TAG, "mmversion = " + mmVersion);
        if (TextUtils.isEmpty(mmVersion)) return "";
        String signinStatus = "";
        switch (mmVersion) {
            case "6.7.3":
                signinStatus = isSignin("com.tencent.mm:id/drp", "com.tencent.mm:id/dro");
                break;
            case "6.6.7":
                signinStatus = isSignin("com.tencent.mm:id/d75", "com.tencent.mm:id/d74");
                break;
            default:
                break;
        }
        return signinStatus;
    }
}
